package com.jkgh.remedium.objects.messages;

import java.util.ArrayList;
import java.util.List;

import com.jkgh.asin.StateMachine;
import com.jkgh.remedium.RemediumConstants;
import com.jkgh.remedium.RemediumServer;
import com.jkgh.remedium.objects.RemediumObject;
import com.jkgh.remedium.objects.messages.in.RemediumC2SCaller;
import com.jkgh.remedium.states.Callback;

public abstract class RemediumInMessage {

	private final List<RemediumObject> history;
	private final RemediumServer server;
	
	public RemediumInMessage(RemediumServer server) {
		this.server = server;
		this.history = new ArrayList<RemediumObject>();
	}

	public static RemediumInMessage recognize(RemediumServer server, StateMachine machine) {
		byte messageType = machine.produceByte();
		System.out.println("Message type "+messageType+" arrived");
		switch (messageType) {
		
			case RemediumConstants.C2S_CALLER_TYPE:
				return new RemediumC2SCaller(server);
		}
		throw new RuntimeException("Unexpected message type "+messageType);
	}

	public abstract void consumeContent(StateMachine machine, Callback callback);
	
	public final RemediumObject fromHistory(int index) {
		return history.get(index);
	}

	public final void toHistory(RemediumObject object) {
		history.add(object);
	}

	public RemediumServer getServer() {
		return server;
	}

}
